/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | www.openfoam.com
     \\/     M anipulation  |
-------------------------------------------------------------------------------
    Copyright (C) 2011-2015 OpenFOAM Foundation
    Copyright (C) 2018-2020 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

inline bool Foam::regIOobject::ownedByRegistry() const
{
    return ownedByRegistry_;
}


inline bool Foam::regIOobject::store()
{
    if (checkIn())
    {
        ownedByRegistry_ = true;
    }
    else
    {
        WarningInFunction
            << "Refuse to store unregistered object: " << this->name() << nl;
    }

    return ownedByRegistry_;
}


template<class Type>
inline Type& Foam::regIOobject::store(Type* p)
{
    if (!p)
    {
        FatalErrorInFunction
            << "Object deallocated\n"
            << abort(FatalError);
    }

    const bool ok = p->regIOobject::store();

    if (!ok)
    {
        FatalErrorInFunction
            << "Failed to store pointer: " << p->regIOobject::name()
            << ". Risk of memory leakage\n"
            << abort(FatalError);
    }

    return *p;
}


template<class Type>
inline Type& Foam::regIOobject::store(autoPtr<Type>& aptr)
{
    // Pass management to objectRegistry
    return store(aptr.release());
}


template<class Type>
inline Type& Foam::regIOobject::store(autoPtr<Type>&& aptr)
{
    // Pass management to objectRegistry
    return store(aptr.release());
}


template<class Type>
inline Type& Foam::regIOobject::store(tmp<Type>& tptr)
{
    Type* p = nullptr;

    if (tptr.isTmp())
    {
        // Pass management to objectRegistry
        p = tptr.ptr();

        store(p);

        // Adjust tmp<> access to use the stored reference
        tptr.cref(*p);
    }
    else
    {
        // Taking ownership of a const-ref does not make much sense.
        // - Storing the object won't actually do so, it will be removed
        //   when the original object goes out of scope.
        // - Storing a clone may not be what we want.

        p = tptr.get();

        WarningInFunction
            << "Refuse to store tmp to const-ref: " << p->name()
            << ". Likely indicates a coding error\n";
    }

    return *p;
}


template<class Type>
inline Type& Foam::regIOobject::store(tmp<Type>&& tptr)
{
    // Treat like a normal reference
    return store(tptr);
}


inline void Foam::regIOobject::release(const bool unregister)
{
    ownedByRegistry_ = false;
    if (unregister)
    {
        registered_ = false;
    }
}


inline Foam::label Foam::regIOobject::eventNo() const
{
    return eventNo_;
}

inline Foam::label& Foam::regIOobject::eventNo()
{
    return eventNo_;
}


inline const Foam::labelList& Foam::regIOobject::watchIndices() const
{
    return watchIndices_;
}


inline Foam::labelList& Foam::regIOobject::watchIndices()
{
    return watchIndices_;
}


// ************************************************************************* //
